{% extends "base.html" %}

{% block content %}
<div class="container mt-3">
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb">
            <li class="breadcrumb-item"><a href="{{ url_for('index') }}">首页</a></li>
            <li class="breadcrumb-item"><a href="#">{{ owner.username }}</a></li>
            <li class="breadcrumb-item"><a href="{{ url_for('repo_detail', username=owner.username, repo_name=repo.name) }}">{{ repo.name }}</a></li>
            <li class="breadcrumb-item active">{{ file.name }}</li>
        </ol>
    </nav>

    <div class="d-flex justify-content-between align-items-center mb-3">
        <h2>
            <span>{{ file.name }}</span>
        </h2>
        {% if current_user.id == repo.owner_id or Collaborator.query.filter_by(repository_id=repo.id, user_id=current_user.id, permission='write').first() %}
        <div>
            <a href="{{ url_for('edit_file', username=owner.username, repo_name=repo.name, file_path=file.path) }}" class="btn btn-sm btn-outline-secondary">编辑</a>
            <form action="{{ url_for('delete_file', username=owner.username, repo_name=repo.name, file_path=file.path) }}" method="POST" class="d-inline">
                <button type="submit" class="btn btn-sm btn-outline-danger ms-2" onclick="return confirm('确定要删除此文件吗？')">删除</button>
            </form>
        </div>
        {% endif %}
    </div>

    <div class="card mb-4">
        <div class="card-header">
            <ul class="nav nav-tabs card-header-tabs">
                <li class="nav-item">
                    <a class="nav-link active" href="#">内容</a>
                </li>
                <li class="nav-item">
                    <a class="nav-link" href="#">历史</a>
                </li>
            </ul>
        </div>
        <div class="card-body">
            {% if file.content.startswith('data:') %}
                {# 二进制文件 #}
                <p>这是一个二进制文件</p>
                <a href="data:application/octet-stream;base64,{{ file.content }}" 
                   download="{{ file.name }}" 
                   class="btn btn-primary">
                    下载文件
                </a>
            {% else %}
                {# 文本文件 #}
                <pre><code>{{ file.content }}</code></pre>
            {% endif %}
        </div>
    </div>
</div>
{% endblock %}
